//单例模式一
// class Person{
//     constructor(name){
//         this.name = name;
//         if(!Person.instance){
//             Person.instance = this; //p1
//         }
//         return Person.instance  //p1
//     }

//     sayName(){
//         console.log(this.name)
//     }
// }

// let p1 = new Person('zs');

// let p2 = new Person('lisi');
// let p3 = new Person('lili');
// let p4 = new Person('ww');
// let p5 = new Person('qq');

// console.log(p1===p5);

// console.log(p1.name);

// console.log(p5.name)

//单例模式二

class Person{ 
    constructor(name){
        this.name = name;
    }
    static getInstance(name){
        if(!Person.instance){
            Person.instance = new Person(name);
        }
        return Person.instance
    }
}

let p1 = Person.getInstance('zs')

let p2 = Person.getInstance('lisi')

console.log(p1 === p2);

console.log(p1.name);

console.log(p2.name)

